<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>智能家居数据大屏</title>
    <style>
        h1 {
            text-align: center;
            margin: 20px 0;
        }

        .chart-container {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
        }

        .chart-item {
            width: 50%;
            height: 400px;
            margin-bottom: 20px;
            box-sizing: border-box;
        }
    </style>
</head>
<body>
<h1>智能家居数字大屏</h1>
<div class="chart-container">
    <div id="dataTable1" class="chart-item" style="width: 50%; height: 400px;float: left"></div>
    <div id="dataTable2" class="chart-item" style="width: 50%; height: 400px;float: right;"></div>
    <div style="clear:both;"></div>
</div>
<div class="chart-container">
    <div id="dataTable3" class="chart-item" style="width: 50%; height: 400px;float: left"></div>
    <div id="dataTable4" class="chart-item" style="width: 50%; height: 400px;float: right"></div>
    <div style="clear:both;"></div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.4.1/echarts.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.js"></script>
<script>
    function visualize(index) {
        const lineChartDom = document.getElementById('dataTable' + index);
        const singleLineChart = echarts.init(lineChartDom);
        let lineOption;
        switch (index) {
            case 1: { // 温度传感器
                lineOption = {
                    title: {
                        text: '温度变化',
                        subtext: '数据纯属虚构'
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    toolbox: {
                        show: true,
                        feature: {
                            dataZoom: {
                                yAxisIndex: 'none'
                            },
                            dataView: {readOnly: false},
                            magicType: {type: ['line', 'bar']},
                            restore: {},
                            saveAsImage: {}
                        }
                    },
                    xAxis: {
                        type: 'category',
                        data: []
                    },
                    yAxis: {
                        type: 'value',
                        axisLabel: {
                            formatter: '{value} °C'
                        }
                    },
                    series: [
                        {
                            type: 'line',
                            data: [],
                            markPoint: {
                                data: [
                                    {type: 'max', name: 'Max'},
                                    {type: 'min', name: 'Min'}
                                ]
                            },
                            markLine: {
                                data: [
                                    { type: 'average', name: 'Avg' },
                                    [
                                        {
                                            symbol: 'none',
                                            x: '90%',
                                            yAxis: 'max'
                                        },
                                        {
                                            symbol: 'circle',
                                            label: {
                                                position: 'start',
                                                formatter: 'Max'
                                            },
                                            type: 'max',
                                            name: '最高点'
                                        }
                                    ]
                                ]
                            }
                        }
                    ]
                };
                break;
            }
            case 2: { // 湿度传感器
                lineOption = {
                    title: {
                        text: '空气湿度变化',
                        subtext: '数据纯属虚构'
                    },
                    series: [
                        {
                            type: 'gauge',
                            center: ['50%', '60%'],
                            startAngle: 200,
                            endAngle: -20,
                            min: 0,
                            max: 100,
                            splitNumber: 10,
                            itemStyle: {
                                color: '#FFAB91'
                            },
                            progress: {
                                show: true,
                                width: 30
                            },
                            pointer: {
                                show: false
                            },
                            axisLine: {
                                lineStyle: {
                                    width: 30
                                }
                            },
                            axisTick: {
                                distance: -45,
                                splitNumber: 5,
                                lineStyle: {
                                    width: 2,
                                    color: '#999'
                                }
                            },
                            splitLine: {
                                distance: -52,
                                length: 14,
                                lineStyle: {
                                    width: 3,
                                    color: '#999'
                                }
                            },
                            axisLabel: {
                                distance: -20,
                                color: '#999',
                                fontSize: 20
                            },
                            anchor: {
                                show: false
                            },
                            title: {
                                show: false
                            },
                            detail: {
                                valueAnimation: true,
                                width: '60%',
                                lineHeight: 40,
                                borderRadius: 8,
                                offsetCenter: [0, '-15%'],
                                fontSize: 60,
                                fontWeight: 'bolder',
                                formatter: '{value} %',
                                color: 'inherit'
                            },
                            data: [
                                {
                                    value: 20
                                }
                            ]
                        },
                        {
                            type: 'gauge',
                            center: ['50%', '60%'],
                            startAngle: 200,
                            endAngle: -20,
                            min: 0,
                            max: 100,
                            itemStyle: {
                                color: '#FD7347'
                            },
                            progress: {
                                show: true,
                                width: 8
                            },
                            pointer: {
                                show: false
                            },
                            axisLine: {
                                show: false
                            },
                            axisTick: {
                                show: false
                            },
                            splitLine: {
                                show: false
                            },
                            axisLabel: {
                                show: false
                            },
                            detail: {
                                show: false
                            },
                            data: [
                                {
                                    value: 20
                                }
                            ]
                        }
                    ]
                };
                break;
            }
            case 3: { // 光照传感器
                lineOption = {
                    color: ['#FFBF00'],
                    title: {
                        text: '光照强度变化曲线',
                        subtext: '数据纯属虚构'
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            label: {
                                backgroundColor: '#6a7985'
                            }
                        }
                    },
                    toolbox: {
                        feature: {
                            saveAsImage: {}
                        }
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            boundaryGap: false,
                            data: []
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            axisLabel: {
                                formatter: '{value} Lux'
                            }
                        }
                    ],
                    series: [
                        {
                            type: 'line',
                            stack: 'Total',
                            smooth: true,
                            lineStyle: {
                                width: 0
                            },
                            showSymbol: false,
                            areaStyle: {
                                opacity: 0.8,
                                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                    {
                                        offset: 0,
                                        color: 'rgb(128, 255, 165)'
                                    },
                                    {
                                        offset: 1,
                                        color: 'rgb(1, 191, 236)'
                                    }
                                ])
                            },
                            emphasis: {
                                focus: 'series'
                            },
                            data: [140, 232, 101, 264, 90, 340, 250]
                        }
                    ]
                };
                break;
            }
            case 4: { // 空气质量传感器
                lineOption = {
                    title: {
                        text: '空气质量AQI',
                        subtext: '数据纯属虚构',
                        left: '1%'
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    grid: {
                        left: '5%',
                        right: '15%',
                        bottom: '10%'
                    },
                    xAxis: {},
                    yAxis: {},
                    toolbox: {
                        right: 10,
                        feature: {
                            dataZoom: {
                                yAxisIndex: 'none'
                            },
                            restore: {},
                            saveAsImage: {}
                        }
                    },
                    dataZoom: [
                        {
                            startValue: '2014-06-01'
                        },
                        {
                            type: 'inside'
                        }
                    ],
                    visualMap: {
                        top: 50,
                        right: 10,
                        pieces: [
                            {
                                gt: 0,
                                lte: 50,
                                color: '#93CE07'
                            },
                            {
                                gt: 50,
                                lte: 100,
                                color: '#FBDB0F'
                            },
                            {
                                gt: 100,
                                lte: 150,
                                color: '#FC7D02'
                            },
                            {
                                gt: 150,
                                lte: 200,
                                color: '#FD0100'
                            },
                            {
                                gt: 200,
                                color: '#AA069F'
                            }
                        ],
                        outOfRange: {
                            color: '#999'
                        }
                    },
                    series: [{
                        name: '空气质量AQI',
                        type: 'line',
                        data: [],
                        markLine: {
                            silent: true,
                            lineStyle: {
                                color: '#333'
                            },
                            data: [
                                {
                                    yAxis: 50
                                },
                                {
                                    yAxis: 100
                                },
                                {
                                    yAxis: 150
                                },
                                {
                                    yAxis: 200
                                },
                                {
                                    yAxis: 300
                                }
                            ]
                        }
                    }]
                };
                break;
            }
        }
        fetch("/data/" + index).then(response => response.json()).then(res => {
            if (index === 2) {
                let value = res.map(value => value.data)[res.length - 1];
                console.log(value);
                lineOption.series[0].data[0].value = value;
                lineOption.series[1].data[0].value = value;
            } else {
                lineOption.xAxis.data = res.map(value => value.time);
                lineOption.series[0].data = res.map(value => value.data);
            }
            singleLineChart.setOption(lineOption);
        })
        setInterval(function () {
            $.ajax({
                url: "/data/" + index,
                type: 'GET',
                data: {},
                async: true,
                dataType: "json",
                // 回调函数
                success: function (res) {
                    if (index === 2) {
                        let value = res.map(value => value.data)[res.length - 1];
                        console.log(value);
                        lineOption.series[0].data[0].value = value;
                        lineOption.series[1].data[0].value = value;
                    } else {
                        lineOption.xAxis.data = res.map(value => value.time);
                        lineOption.series[0].data = res.map(value => value.data);
                    }
                    singleLineChart.setOption(lineOption);
                }, error: function () {
                    console.log("error");
                }
            });
        }, 5000);
    }

    for (let i = 1; i <= 4; i++) visualize(i);
</script>
</body>
</html>